package com.skyline.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.dao.ShareDao;
import com.skyline.base.model.Share;
import com.skyline.base.service.ShareService;
import com.skyline.base.type.ShareType;
import com.skyline.common.bean.Page;
import com.skyline.common.util.FreeMarkerUtils;
import com.skyline.feed.service.PersonalFeedService;
import com.skyline.feed.type.FeedType;
import com.skyline.wo.model.Article;
import com.skyline.wo.model.Photo;
import com.skyline.wo.model.Video;
import com.skyline.wo.service.AlbumService;
import com.skyline.wo.service.ArticleService;
import com.skyline.wo.service.VideoService;

@Service("shareService")
public class ShareServiceImpl implements ShareService {

	@Autowired
	private ShareDao shareDao;

	@Autowired
	private ArticleService articleService;

	// @Autowired
	// private ResourceDao resourceDao;

	@Autowired
	private AlbumService albumService;

	@Autowired
	private VideoService videoService;

	@Autowired
	private PersonalFeedService personalFeedService;

	@Override
	public Integer addShare(Long sharerId, String sharerNickname, String sharerPortrait, ShareType shareType, Long resourceId) {
		if (shareType.equals(ShareType.SHARE)) {
			Share s = shareDao.queryShareById(resourceId);
			// System.ou
			if (shareDao.queryCountBySharerTypeResource(sharerId, s.getShareType(), s.getResourceId()) == 0) {
				shareDao.insertShare(sharerId, sharerNickname, sharerPortrait, s.getShareType(), s.getResourceId(), s.getResourceTitle(),
						s.getResourceDigest(), s.getResourceOwnerId(), s.getResourceOwnerNickname(), s.getResourceOwnerPortrait(),
						s.getResourceSourceId(), s.getResourceSoureceName());
				shareDao.updateShareNum(s.getShareType(), s.getResourceId(), s.getResourceOwnerId());
				shareDao.updateShareNum(ShareType.SHARE, s.getId(), s.getSharerId());
				// 分享成功
				return 0;
			} else {
				// 已经分享过了
				return 1;
			}
		} else {
			if (shareDao.queryCountBySharerTypeResource(sharerId, shareType, resourceId) == 0) {

				if (shareType.equals(ShareType.ARTICLE)) {
					this.addArticleShare(sharerId, sharerNickname, sharerPortrait, resourceId);
				} else if (shareType.equals(ShareType.PHOTO)) {
					this.addPhotoShare(sharerId, sharerNickname, sharerPortrait, resourceId);
				} else if (shareType.equals(ShareType.VIDEO)) {
					this.addVideoShare(sharerId, sharerNickname, sharerPortrait, resourceId);
				}
				// else if (shareType.equals(ShareType.SHARE)) {
				// this.addShareShare(sharerId, sharerNickname, sharerPortrait,
				// resourceId);
				// }
				else if (shareType.equals(ShareType.PATH)) {
					this.addPathShare(sharerId, sharerNickname, sharerPortrait, resourceId);
				}
				shareDao.updateShareNum(shareType, resourceId, sharerId);
				return 0;
			} else {
				return 1;
			}
		}

	}

	private void addPhotoShare(Long sharerId, String sharerNickname, String sharerPortrait, Long resourceId) {
		Photo photo = albumService.getPhotoDetailById(resourceId, sharerId, true);// (resourceId);
		if (photo != null) {
			Map<String, Object> valueMap = new HashMap<String, Object>();
			valueMap.put("middlefile", photo.getMiddleFile());
			valueMap.put("ext", photo.getExt());
			String digest = FreeMarkerUtils.getShare(ShareType.PHOTO, valueMap);
			shareDao.insertShare(sharerId, sharerNickname, sharerPortrait, ShareType.PHOTO, resourceId, photo.getAlbumName(), digest,
					photo.getOwnerId(), photo.getOwnerNickname(), photo.getOwnerPortrait(), photo.getAlbumId(), photo.getAlbumName());
		}
	}

	private void addArticleShare(Long sharerId, String sharerNickname, String sharerPortrait, Long resourceId) {
		Article article = articleService.getArticleById(resourceId);
		if (article != null) {
			shareDao.insertShare(sharerId, sharerNickname, sharerPortrait, ShareType.ARTICLE, resourceId, article.getTitle(),
					article.getDigest(), article.getOwnerId(), article.getOwnerNickname(), article.getOwnerPortrait(),
					article.getCategoryId(), article.getCategoryName());
			// resourceDao.updateShareNumOfOne(ResourceType.ARTICLE,
			// resourceId);
		}
	}

	@SuppressWarnings("deprecation")
	private void addVideoShare(Long sharerId, String sharerNickname, String sharerPortrait, Long resourceId) {
		Video video = videoService.getVideoById(resourceId);
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("sharerId", sharerId);
		valueMap.put("videoId", video.getId());
		valueMap.put("videoHtmlCode", video.getHtmlCode());
		valueMap.put("thumbnail", video.getThumbnail());
		valueMap.put("videoSummary", video.getSummary());
		String resourceDigest = FreeMarkerUtils.getVideoDigest( valueMap);
		Long shareId = shareDao.insertShare(sharerId, sharerNickname, sharerPortrait, ShareType.VIDEO, resourceId, video.getTitle(),
				"<img src='" + video.getThumbnail() + "'/><br>" + video.getSummary(), video.getOwnerId(), video.getOwnerNickname(),
				video.getOwnerPortrait(), null, null);
		personalFeedService.addFeed(shareId, video.getTitle(), resourceDigest, sharerId, sharerNickname, sharerPortrait, sharerId,
				FeedType.VIDEO_SHARE);
		// return shareId;
	}

	private void addPathShare(Long sharerId, String sharerNickname, String sharerPortrait, Long resourceId) {

	}

	@Override
	public List<Share> getShareBySharerId(Long sharerId, Page page) {
		return shareDao.queryShareBySharerId(sharerId, page);
	}

	@Override
	public List<Share> getShareBySharerIdAndType(Long sharerId, ShareType shareType, Page page) {
		return shareDao.queryShareBySharerIdAndType(sharerId, shareType, page);
	}

	@Override
	public Share getShareById(Long id) {
		Share share = shareDao.queryShareById(id);
		if (share != null) {
			if (share.getShareType().equals(ShareType.ARTICLE)) {
				Article article = articleService.getArticleById(share.getResourceId());
				if (article != null) {
					share.setResourceContent(article.getContent());
					share.setTotalShareNum(article.getShareNum());
				}
			}
		}
		return share;
	}

	@Override
	public void deleteShare(Long id, Long actionerId) {
		shareDao.deleteShare(id, actionerId);

	}

	@Override
	public void up(Long id) {
		shareDao.updateUp(id);

	}

	@Override
	public void down(Long id) {
		shareDao.updateDown(id);
	}

}
